<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" th:src="@{/js/layui/layui.js}"></script>
    <link rel="stylesheet" th:href="@{/js/layui/css/layui.css}" type="text/css" />
</head>
<body>

<div>
        费用  <input type="text"  id="price" name="price" value="30"/>
        订单描述  <input type="text"  id="describe" name="describe" value="购买商品"/>
        <button type="button" onclick="toAlipayForm()" >提交订单</button>
</div>
</body>
</html>
<script>

    layui.use([ 'layer'], function() {
        var $ = layui.$;
         toAlipayForm=function (){
             var data=JSON.stringify({
                 "price":$('#price').val(),
                 "describe":$('#describe').val()
             })
             $.ajax({
                 url: '/pay/createOrder',
                 data: data,
                 contentType:'application/json;charset=utf-8',
                 type: 'POST', //HTTP请求类型
                 dataType:"json",
                 success: function (res) {
                     //获得alipay的页面，跳转
                     const div=document.createElement("div")
                     div.innerHTML=res.data;
                     document.body.appendChild(div);
                     document.forms[0].setAttribute("target","_self")
                     document.forms[0].submit()
                 }, error: function () {

                 }
             })
        }

        //获取地址栏某参数的值
        getQueryString=function (name) {
            {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) return unescape(r[2]);
                return null;
            }
        }

        //根据订单号进行查询结果
        searchPaySuccess=function (param){
            $.ajax({
                url: '/pay/queryOutTradeNo?outTradeNo='+param,
                contentType:'application/json;charset=utf-8',
                type: 'GET', //HTTP请求类型
                dataType:"json",
                success: function (res) {
                    alert(res.msg)
                }, error: function () {

                }
            })
        }


        let outTradeNo=getQueryString("out_trade_no");
        if(outTradeNo!=null){
            searchPaySuccess(outTradeNo)
        }

    })
</script>